package com.block.datapush.form.item.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;

public class HaoHuiTuiUtil {

    public static String signParam(Map<String, Object> paramMap, String secretKey){

        List<String> keyList = JSON.parseArray(JSON.toJSONString(paramMap.keySet().toArray()), String.class);

        Collections.sort(keyList);

        StringBuilder stringBuilder =new StringBuilder();
        for (String key: keyList){
            Object value = paramMap.get(key);

            if (ObjectUtil.isEmpty(value)){
                continue;
            }

            stringBuilder.append(StrUtil.format("{}={}&", key, value));
        }
        stringBuilder.append(StrUtil.format("secretKey={}", secretKey));

        System.out.println();
        System.out.println(stringBuilder.toString());
        System.out.println();
        return SecureUtil.md5(stringBuilder.toString()).toUpperCase();
    }

    /**
     * 加密
     *
     * @param input 输入
     * @param aesKey   关键
     * @return {@code String}
     */
    public static String encrypt(String input, String aesKey) {
        byte[] crypted = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(aesKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(crypted);
    }

    public static String decrypt(String input, String aesKey) {
        byte[] output = null;
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            SecretKeySpec skey = new SecretKeySpec(aesKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(decoder.decode(input.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(output);
    }

    public static String getLoanAmount(String loanAmount) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loanAmount)){
            return "I";
        }else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loanAmount)){
            return "I";
        }
        return "J";
    }

    public static String getSheBao(String shebao) {
        if (StrUtil.equals(BaseTypeInfoEnum.SocialInsurance.si001.getValue(), shebao)){
            return "BB";
        }
        return "AA";
    }

    public static String getGjj(String gjj) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gjj)){
            return "FF";
        }
        return "EE";
    }

    public static String getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)){
            return "II";
        }
        return "GG";
    }

    public static String getHouseType(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)){
            return "";
        }
        return "EEE";
    }

    public static String getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)){
            return "LL";
        }
        return "JJ";
    }

    public static String getCarType(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)){
            return "";
        }
        return "HHH";
    }

    public static String getCarAmount(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)){
            return "";
        }
        return "JJJ";
    }

    public static String getBaoDan(String baodan) {
        if (StrUtil.equals(BaseTypeInfoEnum.Insurance.in001.getValue(), baodan)){
            return "NN";
        }
        return "LLL";
    }

    public static String getZhiMa(String zhima) {
        if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm002.getValue(), zhima)){
            return "OO";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm003.getValue(), zhima)){
            return "PP";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm004.getValue(), zhima)){
            return "DDD";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm005.getValue(), zhima)){
            return "QQ";
        }
        return "RR";
    }

    public static String getYuQi(String yuqi) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq002.getValue(), yuqi)){
            return "YY";
        }
        return "ZZ";
    }

    public static String getBaiTiao(String baitiao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt002.getValue(), baitiao)){
            return "PPP";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt003.getValue(), baitiao)){
            return "PPP";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt004.getValue(), baitiao)){
            return "PPP";
        }
        return "QQQ";
    }

    public static String getHuaBei(String huabei) {
        if (StrUtil.equals(BaseTypeInfoEnum.Huabei.hb001.getValue(), huabei)){
            return "TTT";
        }
        return "SSS";
    }

}
